package com.app.medicalcontrol.fragment.Robot.child.item;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;

import com.app.medicalcontrol.R;
import com.app.medicalcontrol.basis.BasisFragment;
import com.app.medicalcontrol.basis.Constants;
import com.app.medicalcontrol.bean.Robot;
import com.app.medicalcontrol.event.RobotListRefreshEvent;
import com.app.medicalcontrol.event.RobotStatusRefreshEvent;
import com.app.medicalcontrol.fragment.Robot.child.RobotItemFragment;
import com.app.medicalcontrol.fragment.WebViewX5Fragment;
import com.loopj.android.http.RequestParams;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

/**
 * Created by otis on 2018/4/11.
 */

public class RobotStatusFragment extends BasisFragment {

    @BindView(R.id.rb_open_status)
    RadioButton openStatus;
    @BindView(R.id.rb_left_work_status)
    RadioButton leftWorkStatus;
    @BindView(R.id.rb_right_work_status)
    RadioButton rightWorkStatus;
    @BindView(R.id.rb_left_ware_status)
    RadioButton leftWareStatus;
    @BindView(R.id.rb_right_ware_status)
    RadioButton rightWareStatus;

    public static RobotStatusFragment newInstance() {
        RobotStatusFragment fragment = new RobotStatusFragment();
        return fragment;
    }

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.fragment_robot_status);
        setTitle(getString(R.string.robot_status));
        setTitleRightButton(R.mipmap.top_icon_msg, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getMessageInfo(v);
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        Log.d("RobotStatusFragment", "initData");
        getList();
    }

    private void getList() {
        showProgress();
        RequestParams params = new RequestParams();
        params.put("robot_id",RobotItemFragment.robotId);
        HttpRestClient.get(Constants.URL_ROBOT_INFO, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        Robot bean = (Robot) result;
                        setView(bean);
                    }
                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                    }
                },
                0, Robot.class);
    }

    private void setView(Robot bean){
        openStatus.setChecked(bean.checkFromInt(bean.getStart_status()));
        openStatus.setText(bean.startOrCloseFromInt(bean.getStart_status()));

        leftWorkStatus.setChecked(bean.checkFromInt(bean.getLeft().getWork_status()));
        leftWorkStatus.setText(bean.serverStatusFromInt(bean.getLeft().getWork_status()));

        leftWareStatus.setChecked(bean.checkFromInt(bean.getLeft().getHardware_status()));
        leftWareStatus.setText(bean.wareStatusFromInt(bean.getLeft().getHardware_status()));

        rightWorkStatus.setChecked(bean.checkFromInt(bean.getRight().getWork_status()));
        rightWorkStatus.setText(bean.serverStatusFromInt(bean.getRight().getWork_status()));

        rightWareStatus.setChecked(bean.checkFromInt(bean.getRight().getHardware_status()));
        rightWareStatus.setText(bean.wareStatusFromInt(bean.getRight().getHardware_status()));

    }

    //订阅方法，当接收到事件的时候，会调用该方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(RobotStatusRefreshEvent messageEvent) {
        getList();
    }


}
